Skip to content

Add set_ticket_key_callback (SSL_CTX_set_tlsext_ticket_key_cb) #330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

toidiu
Copy link

@toidiu toidiu commented Mar 11, 2025

Add a wrapper for the SSL_CTX_set_tlsext_ticket_key_cb, which allows consumers to configure the EVP_CIPHER_CTX and HMAC_CTX used for encrypting/decrypting session tickets.

See https://docs.openssl.org/1.0.2/man3/SSL_CTX_set_tlsext_ticket_key_cb/ for more details.

@toidiu toidiu marked this pull request as ready for review March 11, 2025 04:29
@toidiu toidiu force-pushed the toidiu/ticket_key_callback branch from 5a54a4d to 28f7678 Compare March 11, 2025 19:22
@toidiu toidiu requested a review from rushilmehra March 11, 2025 21:22
toidiu added 4 commits April 1, 2025 12:20
Add a wrapper for the `SSL_CTX_set_tlsext_ticket_key_cb`, which allows
consumers to configure the EVP_CIPHER_CTX and HMAC_CTX used for
encrypting/decrypting session tickets.

See https://docs.openssl.org/1.0.2/man3/SSL_CTX_set_tlsext_ticket_key_cb/
for more details.
@toidiu toidiu force-pushed the toidiu/ticket_key_callback branch from 28f7678 to 9a6e166 Compare April 1, 2025 19:20
F: Fn(
&SslRef,
&mut [u8; 16],
*mut u8,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The IV is constrained to a max length (https://github.com/google/boringssl/blob/main/ssl/ssl_session.cc#L331-L335) so instead of giving applications a raw pointer we should handle the unsafe conversion to a slice constrained to ffi::EVP_MAX_IV_LENGTH ourselves

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea good call. I wasn't sure if the length was dependent on the cipher.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When encrypting a new ticket, encrypt will be one. It writes a public 16-byte key name to key_name and a fresh IV to iv.

Just to confirm, are we ok looking at BoringSSL implementation details to derive this API? The docs, quoted above only guarantee a 16 byte key name and a fresh IV (unspecified length).

Copy link
Collaborator

@kornelski kornelski May 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The key_name and iv are uninitialized, so they can't be u8 arrays/slices! If they had to be slices, then &mut [MaybeUninit<u8>; 16] would be appropriate.

The iv array can use boring_sys::EVP_MAX_IV_LENGTH for the length.

We can't force the callback initialize those fields. I think that makes it unsafe in Rust terms, because safe Rust code could leave them as-is, report success, and make boring read uninit bytes.

The Rust wrapper of this callback needs to first clear these fields (MaybeUninit would work, it may be easier to just call ptr::write(iv, [0; 16])). Then they could be cast to &mut [u8; 16] references.

&SslRef,
&mut [u8; 16],
*mut u8,
*mut ffi::EVP_CIPHER_CTX,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's unfortunate that we have to pass these as raw pointers to users. How feasible is it to create safe bindings for these? If we do this then we'll have another task in our list for the 5.0 major bump

Copy link
Author

@toidiu toidiu Apr 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

callback configures hmac_ctx with an HMAC digest and key, and configures ctx for encryption or decryption, based on the mode.

So based on the docs the callback needs to configure the hmac and enc contexts.

For safe bindings I think we would need to expose a safe wrapper around EVP_CipherInit_ex, EVP_DecryptInit_ex and HMAC_Init_ex and also make it extensible enough for various uses that customers might want.

From what I can tell its not trivial and would probably be a separate task than this PR since its complex enough. That being said, I am not familiar with the 5.0 major bump initiative so can't comment on if its better to wait for safe bindings or if its ok to merge this now.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To unblock this, we can make it pub unsafe fn set_ticket_key_callback_unsafe. The first unsafe, because we can't force the callback to do the correct initialization. The _unsafe suffix to leave room for a future set_ticket_key_callback that may have the right abstractions.

Copy link
Collaborator

@kornelski kornelski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

key_name is uninitialized, so it can't be &mut [u8; 16]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants